<?php
namespace Admin\Controller;
use Tools\AdminController,
    Model\RoundModel;

class RoundController extends AdminController {
    function showlist(){
        $rdmd = new RoundModel();
       
        //首页轮播图
        $list = $rdmd -> where('round_f_id=1') -> order('round_sort asc') -> select();
        //新闻页轮播图
        $nlist =  $rdmd -> where('round_f_id=2') -> order('round_sort asc') -> select();
       
        $this -> assign('list',$list);
        $this -> assign('nlist',$nlist);
        $this -> display();
    }

    
    function add(){
        $rdmd = new RoundModel();
        
        if (!empty($_POST)) { 
            //上传文件处理
            if ($_FILES['round_address']['error'] < 4) {
                $config = array(
                    'rootPath' => "Admin/Public/uploads/", //保存根路径
                    'maxSize' => 2000000, //上传的文件大小限制
                    'exts' => array('jpg', 'gif', 'png', 'jpeg'), //允许上传的文件后缀
                );
                $upload = new \Think\Upload($config);
                //文件上传保存，返回附件的相关信息
                $up = $upload->uploadOne($_FILES['round_address']);
                if ($up) {

                    //图片上传成功后获得图片路径
                    $_POST['round_address'] = 'http://' . $_SERVER['HTTP_HOST'] . '/Show/' . $upload->rootPath . $up['savepath'] . $up['savename'];
                }else{
                    $error = $upload -> getError();
                    $this->redirect('add', '', 2,$error);
                }
            }else {
                $this->redirect('add', '', 2,'请上传图片');
            }
            
            //存储
            if($rdmd->add($_POST)){
                 $this->redirect('showlist');
            }else{
                $this->redirect('showlist');
            }          
        }       
        $this -> display();
    }
    
    
    function delete($id){
        $rdmd = new RoundModel();
        
        if($rdmd -> delete($id)){
            $this ->redirect('showlist');
        }else{
            $this ->redirect('showlist','',3,'删除失败');
        }
    }
    
    
    function xiugai($id){
        $rdmd = new RoundModel();
        
        $round = $rdmd -> find($id);
        
        if (!empty($_POST)) { 
            //上传文件处理
            if ($_FILES['round_address']['error'] < 4) {
                $config = array(
                    'rootPath' => "Admin/Public/uploads/", //保存根路径
                    'maxSize' => 2000000, //上传的文件大小限制
                    'exts' => array('jpg', 'gif', 'png', 'jpeg'), //允许上传的文件后缀
                );
                $upload = new \Think\Upload($config);
                //文件上传保存，返回附件的相关信息
                $up = $upload->uploadOne($_FILES['round_address']);
                if ($up) {
                    
                    //旧图片删除
                     $supl = 'http://'.$_SERVER['HTTP_HOST'].'/Show/';
                     $pic = ltrim($round['round_address'],$supl);
                     unlink($pic);
                     
                    //图片上传成功后获得图片路径
                    $_POST['round_address'] = 'http://' . $_SERVER['HTTP_HOST'] . '/Show/' . $upload->rootPath . $up['savepath'] . $up['savename'];
                }else{
                    $error = $upload -> getError();
                    $this->redirect('xiugai', '', 2,$error);
                }
            }
            
            if($rdmd->save($_POST)){
                 $this->redirect('showlist');
            }else{
                $this->redirect('showlist');
            }            
        }
             
        $this -> assign('round',$round);
        $this -> display();
    }
    
    
    function sort_change($id,$sort){
        $rdmd = new RoundModel();
        
        $up['round_id']   = $id;
        $up['round_sort'] = $sort;
        
        $shu = $rdmd -> create($up);
        if($shu){
            $rdmd -> save($shu);
        }else{
            echo $rdmd -> getError();
        }    
    }
    
}